<html>
<head><meta charset="utf-8"><title>asm_snippet! · project-inline-asm · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/index.html">project-inline-asm</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html">asm_snippet!</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232865507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/asm_snippet%21/near/232865507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html#232865507">(Apr 02 2021 at 06:23)</a>:</h4>
<p>I'm considering adding <code>asm_snippet!</code> which can be used to define bits of asm code that can be used within a larger <code>asm!</code> block</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">macro_rules!</span><span class="w"> </span><span class="n">snippet</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="cp">$arg</span>:<span class="nc">expr</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">asm_snippet</span><span class="o">!</span><span class="p">(</span><span class="w"></span>
<span class="w">            </span><span class="c1">// Template code</span>
<span class="w">            </span><span class="s">"mul {arg}, {arg}"</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="c1">// Snippet can define arguments (named only)</span>
<span class="w">            </span><span class="n">arg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">inout</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span><span class="w"> </span><span class="cp">$arg</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="fm">asm!</span><span class="p">(</span><span class="w"></span>
<span class="w">    </span><span class="n">snippet</span><span class="o">!</span><span class="p">(</span><span class="n">arg</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="c1">// You can use arguments defined in the snippet outside of it</span>
<span class="w">    </span><span class="s">"add {arg}, {arg}"</span><span class="p">,</span><span class="w"></span>
<span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="232865619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/asm_snippet%21/near/232865619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html#232865619">(Apr 02 2021 at 06:24)</a>:</h4>
<p>For a more complex example, consider the new/old code <a href="https://gist.github.com/Amanieu/d97c420c00cd51fad556b633e0d87af1">here</a>.</p>



<a name="232865789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/asm_snippet%21/near/232865789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html#232865789">(Apr 02 2021 at 06:27)</a>:</h4>
<p>This is also partially motivated by a desire to better handle labels in asm using special syntax as proposed <a href="https://github.com/rust-lang/rust/issues/81088#issuecomment-773556634">here</a>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">asm!</span><span class="p">(</span><span class="n">foo</span>: <span class="s">"jmp {foo}"</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="232865808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/asm_snippet%21/near/232865808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html#232865808">(Apr 02 2021 at 06:27)</a>:</h4>
<p>This doesn't work with the old method for asm snippets where we can only import a string literal from a macro (see the first linked example).</p>



<a name="232883023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/asm_snippet%21/near/232883023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html#232883023">(Apr 02 2021 at 10:15)</a>:</h4>
<p>So, an asm snippet is a self-contained bundle of assembly, with parameters and labels encapsulated?</p>



<a name="232883189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/asm_snippet%21/near/232883189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html#232883189">(Apr 02 2021 at 10:17)</a>:</h4>
<p>Also, what is the motivation for allowing use of snippet arguments from outside the snippet? Shouldn't they have to be namespaced relative to the snippet?</p>



<a name="232883260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/asm_snippet%21/near/232883260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html#232883260">(Apr 02 2021 at 10:18)</a>:</h4>
<p>(As in, snippet_name.arg or similar.)</p>



<a name="232883293"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/asm_snippet%21/near/232883293" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html#232883293">(Apr 02 2021 at 10:19)</a>:</h4>
<p>Snippets don't have a name.</p>



<a name="232883299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/asm_snippet%21/near/232883299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html#232883299">(Apr 02 2021 at 10:19)</a>:</h4>
<p>I'm wondering if it would make sense to have snippets be a named argument and then substituted into the assembly.</p>



<a name="232883317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/asm_snippet%21/near/232883317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html#232883317">(Apr 02 2021 at 10:19)</a>:</h4>
<p>(Was in the process of writing that.)</p>



<a name="232883329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216763-project-inline-asm/topic/asm_snippet%21/near/232883329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/216763-project-inline-asm/topic/asm_snippet!.html#232883329">(Apr 02 2021 at 10:19)</a>:</h4>
<p>And the intent is that you could share a single operand between the snippet and the main assembly.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>